<cfcomponent displayname="projectController" extends="ModelGlue.unity.controller.Controller" output="false">

	<cffunction name="setProjectService" access="public" returntype="void" output="false">	
		<cfargument name="projectService" type= "any" required="true">	
		<cfset variables.projectService = arguments.projectService />
	</cffunction>

	<cffunction name="getProjectService" access= "public" returntype="any" output="false">	
		<cfreturn variables.projectService />
	</cffunction>

	<cffunction name="setTicketsGrid" access="public" returntype="void" output="false">	
		<cfargument name="TicketsGrid" type= "any" required="true">	
		<cfset variables.TicketsGrid = arguments.TicketsGrid />
	</cffunction>

	<cffunction name="getTicketsGrid" access= "public" returntype="any" output="false">	
		<cfreturn variables.TicketsGrid />
	</cffunction>

	<cffunction name="setTracRpcAdapter" access="public" returntype="void" output="false">	
		<cfargument name="TracRpcAdapter" type= "any" required="true">	
		<cfset variables.TracRpcAdapter = arguments.TracRpcAdapter />
	</cffunction>

	<cffunction name="getTracRpcAdapter" access= "public" returntype="any" output="false">	
		<cfreturn variables.TracRpcAdapter />
	</cffunction>

	<cffunction name="getProjectshenORMService" output="false">
	  <cfreturn beans.projectshenORMService />
	</cffunction>

	<cffunction name="setDojoAdapter" access="public" returntype="void" output="false">	
		<cfargument name="DojoAdapter" type= "any" required="true">	
		<cfset variables.DojoAdapter = arguments.DojoAdapter />
	</cffunction>

	<cffunction name="getDojoAdapter" access= "public" returntype="any" output="false">	
		<cfreturn variables.DojoAdapter />
	</cffunction>

	<cffunction name="addMapping">
		<cfargument name="ModelGlue" required="true" type="any" />
		<cfargument name="thisViewMapping">
		<cfset var viewMappings = arguments.ModelGlue.getConfigSetting("viewMappings") />
		<cfif isArray(viewMappings)>
		  <cfset viewMappings = arrayToList(viewMappings) />
			<cfif NOT listFind(viewMappings,thisViewMapping)>
				<cfset modelglue.getViewRenderer().addViewMapping(thisViewMapping) />
			</cfif>	
		<cfelse>
			<cfif NOT listFind(viewMappings,thisViewMapping)>
				<cfset arguments.ModelGlue.setConfigSetting("viewMappings",listAppend(arguments.ModelGlue.getConfigSetting("viewMappings"),thisViewMapping))>
			</cfif>	
		</cfif>
	</cffunction>
	
	<cffunction name="dumpvar">
		<cfargument name="daVar"><cfargument name="abort" default="true">
		<cfdump var="#daVar#"><cfif arguments.abort><cfabort /></cfif>
	</cffunction>

	<cffunction name="init" access="public" returntype="any" hint="object constructor.">
		<cfargument name="ModelGlue" required="true" type="ModelGlue.ModelGlue" />
		<cfargument name="InstanceName" required="true" type="string" />
		<cfset super.init( arguments.ModelGlue ) />
		<!--- <cfset addMapping(ModelGlue,"/actionpacks/projectshen/views" ) /> --->
		<cfreturn this />
	</cffunction>

	<cffunction name="whackReactorIfNeeded" access="public" returnType="void" output="false">
	  <cfargument name="event" type="any">
	  <cfset var project = "" />
	  <cfif arguments.event.getValue("whackreactor") gt "">
		  <cfset project = getProjectshenORMService().getConfiguration().getProject() />
			<cftry>
				<cfdirectory action="delete" directory="#expandPath("/reactor/project/#project#")#" recurse="true">
			<cfcatch>
				Unable to delete
				<cfdump var="#cfcatch#">
				<cfabort />
			</cfcatch>
			</cftry>
			<cfset getProjectshenORMService().compile() />			
		</cfif>
	</cffunction>

	<cffunction name="getProjectRecord" access="private" returntype="any" hint="gets project">
	  <cfargument name="project" type="any">
	  <cfscript>
			var projectRecord = getProjectshenORMService().createRecord("projects");
			if (isNumeric(project)) {
				projectRecord.load(projectID = arguments.project);
			} else {
				projectRecord.load(projectName = rereplacenocase(arguments.project,'[^a-z0-9_]','','all'));
			}
			return projectRecord;
		</cfscript>
	</cffunction>

	<cffunction name="getProject" access="public" returntype="any" hint="gets project">
	  <cfargument name="event" type="any">
	  <cfscript>
			var projectRecord = getProjectRecord(arguments.event.getValue("projectId",arguments.event.getValue("project")));
			arguments.event.setValue("projectRecord",projectRecord);
			arguments.event.setValue("projectId",projectRecord.getProjectId());
		</cfscript>
	</cffunction>

	<cffunction name="getProjects" access="public" returntype="any" hint="gets project">
	  <cfargument name="event" type="any">
	  <cfscript>
			arguments.event.setValue("projects",getProjectService().getProjectsQuery());
		</cfscript>
	</cffunction>

	<cffunction name="setProjectConfigDefaults" access="public" returntype="any" hint="gets project">
	  <cfargument name="event" type="any">
	  <cfscript>
			var projectId = getProjectRecord(arguments.event.getValue("project")).getProjectId();
			getProjectService().setProjectConfigDefaults(projectId);
		</cfscript>
	</cffunction>

	<cffunction name="xmlrpc" access="public" returntype="any" hint="gets project">
	  <cfargument name="event" type="any">
	  <cfset var projectRecord = getProjectRecord(arguments.event.getValue("project",arguments.event.getValue("projectId")))>
	  <cfset var method = arguments.event.getValue("method")>
		<cfparam default="" name="server.rcpLog">
	  <cfif method eq "/xmlrpc/login" OR method eq "/login/xmlrpc">
	    <cftry>
		    <cfset arguments.event.setValue("xmlRpcResponse",getTracRpcAdapter().runCalledMethod(projectRecord.getProjectId()))>
			<cfcatch>
				<cfsavecontent variable="we"><cfdump var="#cfcatch#"></cfsavecontent>
				<cfset server.rcpLog = server.rcpLog & GetHttpRequestData().content>
				<cfset server.rcpLog = server.rcpLog & cfcatch>
				<cfset server.rcpLog = server.rcpLog & we>
				<cfset server.rcpLog = server.rcpLog & "<br />">
			</cfcatch>
			</cftry>

	  <cfelseif method eq "/" OR method eq "" OR method eq "/xmlrpc">
	    <!--- gets version info --->
	    <cfset arguments.event.setValue("xmlRpcResponse",getTracRpcAdapter().runCalledMethod(projectRecord.getProjectId()))>

		<cfelseif listFirst(method,"/") eq "wiki">
			WikiWOrd = <cfoutput>#method#</cfoutput>
	    <cfdump var="#method#">
	    <cfdump var="#url#"><cfabort >
		<cfelseif listFirst(method,"/") eq "changeset">
	    <cfset getProjectService().verifyAuthentication()>
	    <cfdump var="#method#">
	    <cfdump var="#url#">
	    <cfabort >
	    <cfset arguments.event.setValue("firstRevision",listGetAt(method,2,"/"))>
	    <cfset arguments.event.setValue("lastRevision",listGetAt(method,2,"/"))>
	    <cfset arguments.event.forward("repo.changeSet","firstRevision,lastRevision")>
<!--- 	    <cfset getProjectService().viewChangeSet(listGetAt(method,2,"/"))>
 --->		
	  <cfelseif listFirst(method,"/") eq "attachment">
	    <cfset getProjectService().viewTicketAttachment(listGetAt(method,3,"/"),listGetAt(method,4,"/"))>
		</cfif>

<!---
 		<cfsavecontent variable="dumpCont">
			<cfdump var="#event.getAllValues()#">
			<cfdump var="#GetHttpRequestData().content#">
			<cftry>
				<cfdump var="#xmlParse(GetHttpRequestData().content)#">
				<cfdump var="#xmlParse(arguments.event.getValue("xmlRpcResponse"))#">
			<cfcatch></cfcatch>
			</cftry>
		</cfsavecontent>
		<cfset server.rcpLog = server.rcpLog & dumpCont>
 --->
	</cffunction>

	<cffunction name="json" access="public" returntype="any" hint="gets project">
	  <cfargument name="event" type="any">
	  <cfset var projectRecord = getProjectRecord(arguments.event.getValue("project")) />
	  <cfset var method = arguments.event.getValue("method") />
    <cfset arguments.event.setValue("json",getDojoAdapter().runCalledMethod(projectRecord.getProjectId(),method,url)) />
	</cffunction>

	<cffunction name="getTicketsGridData" access="public" returntype="any" hint="gets project">
	  <cfargument name="event" type="any">
	  <cfscript>
			var ticketsGrid = getTicketsGrid();
			var gridRowVals = structNew();
			var command = arguments.event.getValue("command","info");
			var x = 0;
			var desc = "";
			var eventvals = arguments.event.getAllValues();
			var filters = event.getValue("filters");
			if(command eq "info") {
				command =  ticketsGrid.getGridDataInfo();
				arguments.event.getValue("json",command);
			} 
			else if (command eq "select") {
				desc = event.getValue("desc",false);
				desc = replace(desc,"'","","all");
				if(desc neq "true") { 
					desc = false; 
				}
				command =  ticketsGrid.getGridData(filters,event.getValue("orderby"),desc);
				arguments.event.setValue("json",command);
			}
			else if (command eq "update") {
				while (structKeyExists(eventvals,"_"&x)) {
					gridRowVals[x&"old"] = eventvals["_o" & x];
					gridRowVals[x&"new"] = eventvals["_" & x];
					x = x+1;
				}
				command =  ticketsGrid.setGridData(gridRowVals);
				arguments.event.setValue("json",command);
			}
		</cfscript>
	</cffunction>

</cfcomponent>